package com.system.vote.admin.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.system.vote.admin.entity.TVoteDetail;
import com.system.vote.admin.entity.TVoteItem;
import com.system.vote.admin.exception.BusinessException;
import com.system.vote.admin.security.JwtUtil;
import com.system.vote.admin.service.TVoteDetailService;
import com.system.vote.admin.service.TVoteItemService;
import com.system.vote.core.http.Result;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

@RestController
@RequestMapping("/app/vote/tvotedetail")
@Api(tags = "App投票详情管理")
public class AppTVoteDetailController {

    @Autowired
    private TVoteItemService tVoteItemService;

    @Autowired
    private TVoteDetailService tVoteDetailService;

    /**
     * 添加用户投票信息
     *
     * @param request
     * @param tVoteDetail
     * @return
     */
    @Transactional
    @PostMapping("/save")
    public Result save(HttpServletRequest request, @RequestBody TVoteDetail tVoteDetail) {
        String token = request.getHeader("token");
        Claims claims = JwtUtil.parseJWT(token);
        String uid = claims.getId();
        tVoteDetail.setVoteDate(new Date());
        tVoteDetail.setWUid(Long.valueOf(uid));

        //用户只能投票一次
        int count = tVoteDetailService.count(new LambdaQueryWrapper<TVoteDetail>().eq(TVoteDetail::getWUid, uid).and(e -> e.eq(TVoteDetail::getVoteId, tVoteDetail.getVoteId())));
        if (count > 0) {
            throw new BusinessException("你已经投过了，不能重复投票");
        } else {
            tVoteDetailService.save(tVoteDetail);
            TVoteItem tVoteItem = tVoteItemService.getOne(new QueryWrapper<TVoteItem>().eq("id", tVoteDetail.getVoteItemId()));
            tVoteItem.setNumber(tVoteItem.getNumber() + 1);
            //某项投票次数添加1
            tVoteItemService.updateById(tVoteItem);

            Map<String, Object> map = new HashMap<>();
            map.put("flag", true);
            return Result.success(map);
        }


    }

    @GetMapping("/list/{id}")
    public Result listDetailItems(@PathVariable Long id) {
        List<TVoteItem> tVoteItemList = tVoteItemService.list(new LambdaQueryWrapper<TVoteItem>().eq(TVoteItem::getVoteId, id).orderByDesc(TVoteItem::getNumber));
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("list", tVoteItemList);
        return Result.success(resultMap);
    }

    @GetMapping("/{id}")
    public Result getItemDetail(@PathVariable("id") Long id) {
       TVoteItem tVoteItem=tVoteItemService.getOne(new QueryWrapper<TVoteItem>().eq("id",id));
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("objData", tVoteItem);
        return Result.success(resultMap);
    }
}
